<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import s94Mjs from "s94-js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      group: {label:'配置组',name:'group',type:'hidden'},
      name: {label:'配置名',name:'name',type:'hidden'},
      label: {label:'配置名',name:'label',tips:''},
      value: {label:'值',name:'value',tips:''},
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,

      key: this.$get('key', 'null'),
      group: '',
      name: '',
    }
  },
  methods: {
    onsubmit(formData){
      let url = '/admin/config_group/edit_value?group='+encodeURIComponent(this.group);
      url = '/admin/config_group/edit_value_one';
      ajax({url:url, method:'POST', data: formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('config_group/index', '$refs.list_url.list');
          if (this.key == 'page.link.bottom_link'){
            iframe.exe('/index_card/index', 'get_config');
            iframe.exe('/index_card/display', 'change_bottom_link', [formData.get('value')]);
          }
          iframe.hide('config_group/edit_value');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id || this.key) {
      let url = '/admin/config_group/config_map?key=' + encodeURIComponent(this.key);
      ajax({url:url, cache:true}, (res)=>{
        this.group = res.data.group;
        this.name = res.data.name;
        app.config.group.value = res.data.group;
        app.config.name.value = res.data.name;
        app.config.label.value = res.data.label;


        app.config.value.type = res.data.type;
        app.config.value.config = res.data.config;
        app.config.value.tips = res.data.tips;
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('config_group/edit_value');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
